package com.kzw.oa.service.info.impl;

import java.util.Date;
import java.util.Set;

import javax.annotation.Resource;

import com.kzw.core.Constants;
import com.kzw.core.dao.GenericDao;
import com.kzw.core.service.impl.BaseServiceImpl;
import com.kzw.core.util.AppUtil;
import com.kzw.core.util.ContextUtil;
import com.kzw.oa.dao.info.InMessageDao;
import com.kzw.oa.dao.info.NotificationDao;
import com.kzw.oa.dao.info.ShortMessageDao;
import com.kzw.oa.dao.system.AppUserDao;
import com.kzw.oa.model.info.InMessage;
import com.kzw.oa.model.info.Notification;
import com.kzw.oa.model.info.ShortMessage;
import com.kzw.oa.model.system.AppUser;
import com.kzw.oa.service.info.NotificationService;
import com.kzw.oa.service.info.ShortMessageService;

public class NotificationServiceImpl extends BaseServiceImpl<Notification>
		implements NotificationService {
	@Resource
	private ShortMessageDao messageDao;
	@Resource
	private InMessageDao inMessageDao;
	@Resource
	private AppUserDao appUserDao;
	public NotificationServiceImpl(NotificationDao dao) {
		super(dao);
	}
	
	/**public void publish(Notification notification){
		
	}**/
	public void setEnable(String defFlag,boolean enable){
		Integer enflag = enable==true ? 1 : 0;
		dao.update("update Notification notifi set notifi.enabled=? where notifi.defFlag=?",enflag,defFlag);
	}
	/**
	 * 发布定时消息通知，消息需要用户登录的时候激活
	 * @param content
	 * @param userId
	 * @param start
	 * @param end
	 * @param defFlag 定义标识，用于自定义确定某条提醒
	 */
	public void publish(String content,Long userId,Date start,Date end,String defFlag){
		Long senderId=AppUser.SUPER_USER;
		
		ShortMessage shortMessage = new ShortMessage();
		shortMessage.setContent(content);
		shortMessage.setMsgType(ShortMessage.MSG_TYPE_SYS);
		AppUser curUser = appUserDao.get(senderId);
		shortMessage.setSender(curUser.getFullname());
		shortMessage.setSenderId(curUser.getUserId());
		shortMessage.setSendTime(new Date());

		messageDao.save(shortMessage);

		//String[] reIds = receiveIds.split("[,]");
	//	if (reIds != null) {

		//	for (String userId : reIds) {
		InMessage inMsg = new InMessage();
		inMsg.setDelFlag(Constants.FLAG_UNDELETED);
		inMsg.setReadFlag(InMessage.FLAG_UNREAD);
		inMsg.setShortMessage(shortMessage);
		AppUser receiveUser = appUserDao.get(userId);

		inMsg.setUserId(receiveUser.getUserId());
		inMsg.setUserFullname(receiveUser.getFullname());
		inMsg.setReadFlag(InMessage.FLAG_READ);//设置为已读
		inMsg.setDelFlag(Short.valueOf("1"));//
		inMessageDao.save(inMsg);
		Notification notification = new Notification();
		notification.setInMessageId(inMsg.getReceiveId());
		notification.setEnabled(1);
		notification.setStartDate(start);
		notification.setEndDate(end);
		notification.setDefFlag(defFlag);
		notification.setOwnerId(userId);
		dao.save(notification);
			//}
		//}
	}
	/**
	 * 激活个人通知
	 */
	public void activateNotification(){
			Long uid = ContextUtil.getCurrentUserId();
			Date nowDate = new Date();
			//设置到期的通知
			//dao.update("update Notification notifi set notifi.enabled=1 where notifi.startDate < ? and notifi.endDate > ?",nowDate,nowDate);
			//设置超期
			//dao.update("update Notification notifi set notifi.enabled=0 where  notifi.endDate < ?",nowDate);
			//不显示已经超期了并且不在通知范围内的消息
			inMessageDao.update("update InMessage m set m.delFlag=1 where m.receiveId in(select inMessageId From Notification where  ownerId=? and (enabled=0 or endDate < ?))",uid,nowDate);
			inMessageDao.update("update InMessage m set m.readFlag=0 , m.delFlag=0 where m.receiveId in(select inMessageId From Notification where enabled=1 and ownerId=? and  startDate < ? and endDate > ?)",uid,nowDate,nowDate);
			
	}
}
